package com.ss.SmartStock.service;

import com.ss.SmartStock.pojo.entity.Product;
import com.ss.SmartStock.task.ProductTask;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ProductScheduler {
    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    public void scheduleProductOnline(ProductService productService, long productId, long delay) {
        Runnable task = new ProductTask(productService, productId, true);
        scheduler.schedule(task, delay, TimeUnit.SECONDS); // 假设delay是以秒为单位的延迟  
    }

    public void scheduleProductOffline(ProductService productService, long productId, long delay) {
        Runnable task = new ProductTask(productService, productId, false);
        scheduler.schedule(task, delay, TimeUnit.SECONDS); // 假设delay是以秒为单位的延迟  
    }

    // 当不再需要调度器时，应该关闭它以释放资源  
    public void shutdown() {
        scheduler.shutdown();
    }
}